package com.etc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.dao.IUserDao;
import com.etc.dao.impl.UserImpl;
import com.etc.entity.Announcement;
import com.etc.entity.User;
import com.etc.service.impl.AnnouncementServiceImpl;
import com.etc.service.impl.AnsMessageServiceDaoImpl;
import com.etc.service.impl.MessageServiceImpl;
import com.etc.service1.AnnouncementService;
import com.etc.service1.AnsMessageServiceDao;
import com.etc.service1.MessageServiceDao;



/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String op=request.getParameter("op");
		
		//System.out.print(op);
		
		if(op==null) {
			login(request,response);
		}
		else if(op=="reg"||op.equals("reg")){
			reg(request,response);
		}
		else if(op=="chauser"||op.equals("chauser")){
			chauser(request,response);
		}
		else if(op=="feng"||op.equals("feng")){
			feng(request,response);
		}
		else if(op=="jie"||op.equals("jie")){
			jie(request,response);
		}
		else if(op=="update"||op.equals("update")){
			update(request,response);
		}
		
		else if(op=="search"||op.equals("search")){
			search(request,response);
		}
	}
	//��¼
	protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String name=request.getParameter("username");
		String pwd=request.getParameter("password");
		String role = request.getParameter("role");
		String usercheckcode = request.getParameter("checkcode");
		AnnouncementService an=new AnnouncementServiceImpl();
		  List<Announcement> list=an.listAnnouncement();
		MessageServiceDao ms = new MessageServiceImpl();
		AnsMessageServiceDao as = new AnsMessageServiceDaoImpl();
		AnnouncementService ac = new AnnouncementServiceImpl();
		int count = ms.countUsername(name).intValue();
		int count2=as.countAllUser(name).intValue();
		System.out.println(count2);
		int count3=ac.countAll().intValue();
		HttpSession session = request.getSession();
		String servercheckcode = (String) session.getAttribute("checkCode");
	
		if(!servercheckcode.equalsIgnoreCase(usercheckcode))
		{
			request.setAttribute("info","验证码错误");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
		else
		{
			IUserDao dao=new UserImpl();
			User user=(User) dao.findUser(name, pwd, role);
			if(user!=null) 
			{
				if("正常"==user.getStatus()||"正常".equals(user.getStatus()))
				{
					request.getSession().setAttribute("userid", user.getId());
					request.getSession().setAttribute("username", user.getUsername());
					request.getSession().setAttribute("userrole", user.getRole());
					request.getSession().setAttribute("userstatus", user.getStatus());
					request.getSession().setAttribute("password", user.getPassword());
					request.getSession().setAttribute("count", count);
					request.getSession().setAttribute("count2", count2);
					request.getSession().setAttribute("count3", count3);
					request.getSession().setAttribute("AnnouncementList", list);
					/*
					 * if("admin"==user.getRole()||"admin".equals(user.getRole())) {
					 * request.getRequestDispatcher("index_admin.jsp?sf=admin").forward(request,
					 * response); } else {
					 * request.getRequestDispatcher("index_user.jsp?sf=user").forward(request,
					 * response); }
					 */
					request.setAttribute("sf", user.getRole());
					request.getRequestDispatcher("index.jsp").forward(request, response);
				}
				else
				{
					request.setAttribute("info", "用户已被封禁");
					request.getRequestDispatcher("login.jsp").forward(request, response);
				}
			}
			else 
			{
				request.setAttribute("info", "账号或密码错误请重新登录");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		}
	}
	
	protected void reg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String name=request.getParameter("username");
		String pwd=request.getParameter("password");
		String role="user";
		String status="正常";
		
		IUserDao dao=new UserImpl();
		if(dao.regUser(name, pwd,role,status)) {
			request.setAttribute("name", name);
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("regist.jsp").forward(request, response);
		}
	}
	
	protected void chauser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//0.5
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String search = request.getParameter("search");
		
		IUserDao dao=new UserImpl();
		List<User> list = new ArrayList<User>();
		list = dao.searchUser(search);
		request.getSession().setAttribute("userlist", list);
		//System.out.println(list.size());
		request.getSession().setAttribute("search", search);
		request.getRequestDispatcher("cha_user.jsp").forward(request, response);
	}
	
	protected void feng(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String search =(String)request.getSession().getAttribute("search");
		
		int id = Integer.parseInt(request.getParameter("id"));
		IUserDao dao=new UserImpl();
		if(dao.closeUser(id))
		{
			//System.out.print("success");
			chauser(request,response);
		}
		else
			System.out.print("fail");
	}
	
	protected void jie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");

		String search =(String)request.getSession().getAttribute("search");
		
		int id = Integer.parseInt(request.getParameter("id"));
		IUserDao dao=new UserImpl();
		if(dao.openUser(id))
		{
			//System.out.print("success");
			chauser(request,response);
		}
		else
			System.out.print("fail");
	}
	
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//0
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String username=request.getParameter("username");
		String password=request.getParameter("pass");
		String newpassword=request.getParameter("repass");
		IUserDao dao=new UserImpl();
		if(dao.updateUser(username,password,newpassword)) {
			//System.out.print("success");
			request.setAttribute("info", "修改成功");
			request.setAttribute("xiugai", "yes");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}else {
			request.setAttribute("info", "修改失败   ");
			request.setAttribute("xiugai", "no");
			request.getRequestDispatcher("html/update-password.jsp").forward(request, response);
		}
	}
	
	protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
//		String searchtext = request.getParameter("searchtext");
//		IUserDao dao=new UserDaoImpl();
//		List<User> list = new ArrayList<User>();
//		list = dao.searchUser(searchtext);
//		request.setAttribute("list", list);
//		request.setAttribute("search", searchtext);
//		request.getRequestDispatcher("html/admin-list.jsp").forward(request, response);
		IUserDao us = new UserImpl();
		List<User> list = us.searchAllUser();
		request.setAttribute("listUser", list);
		request.getRequestDispatcher("html/admin-list.jsp").forward(request, response);
	}
	
}
